package nxu.service;

import nxu.dao.DbHelper;
import nxu.entity.Post;

import java.util.List;

public class PostService {

    public boolean save (Post post){
        boolean success=false;
        try {
        String insetPostSql="insert into post (pid,title,uid,cid,content)"+
                "values(:pid,:title,:uid,:cid,:content)";
        DbHelper.JDBI.withHandle(handle -> handle.createUpdate(insetPostSql)
                .bindBean(post)
                .execute()
        );
        success=true;
        }catch (Exception e){
            System.out.println("出错");
        }
        return success;
    }

    public List<Post> listByCategory(String cid){
        String listByCategorySql = "select * from post where cid=?";
        return DbHelper.JDBI.withHandle(handle -> handle.createQuery(listByCategorySql)
                .bind(0,cid)
                .mapToBean(Post.class)
                .list()
        );
    }


    public List<Post> view (String pid){
        String listByCategorySql = "select * from post where pid=?";
        return DbHelper.JDBI.withHandle(handle -> handle.createQuery(listByCategorySql)
                .bind(0,pid)
                .mapToBean(Post.class)
                .list()
        );
    }

}
